package com.zx.Ding.controller;/**
 * Author:JasonZ
 * Data:2023/3/17
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.Ding.common.R;
import com.zx.Ding.entity.CategoryDetail;
import com.zx.Ding.entity.Employee;
import com.zx.Ding.service.CategoryDetailService;
import com.zx.Ding.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: Ding
 *
 * @description:
 *
 * @author: JasonZ
 *
 * @create: 2023-03-17 11:58
 **/
@RestController
@RequestMapping("/categoryDetail")
public class CategoryDetailController {
    @Autowired
    private CategoryDetailService categoryDetailService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page>pageList(int page, int pageSize,String categoryName){
        //构造分页构造器
        Page pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<CategoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(CategoryDetail::getScore);
        queryWrapper.orderByDesc(CategoryDetail::getCommentTotal);
        queryWrapper.like(categoryName!=null,CategoryDetail::getCategoryName,categoryName);
        //分页查询
        categoryDetailService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @GetMapping
    public R<List<CategoryDetail>> list(){
        List<CategoryDetail> categoryDetails = categoryDetailService.list();
        return R.success(categoryDetails);
    }

    @GetMapping("/one/{id}")
    public R<CategoryDetail> listById(@PathVariable Long id){
        LambdaQueryWrapper<CategoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,CategoryDetail::getId,id);
        CategoryDetail categoryDetail = categoryDetailService.getOne(queryWrapper);
        return R.success(categoryDetail);
    }

    @GetMapping("/{categoryId}")
    public R<List<CategoryDetail>> listByCategoryId(@PathVariable Long categoryId){
        LambdaQueryWrapper<CategoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,CategoryDetail::getCategoryId,categoryId);
        List<CategoryDetail> list = categoryDetailService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping
    public R<String>sava(@RequestBody CategoryDetail categoryDetail){
        categoryDetailService.save(categoryDetail);
        return R.success("新增员工成功");
    }

    @PutMapping()
    public R<String>update(@RequestBody CategoryDetail categoryDetail){
        categoryDetailService.updateById(categoryDetail);
        return R.success("修改成功");
    }
}